#ifndef __MCS__VARIABLE_EXPRESSION__HPP
#define __MCS__VARIABLE_EXPRESSION__HPP

#include "Expression.hpp"
#include "Variable.hpp"
#include "Block.hpp"
#include "VariableNotFoundException.hpp"

/// Expression that reads content of a single variable
class VariableExpression : public Expression
{
public:
	VariableExpression(SourceLocation const & location, String const & name)
		: Expression(location)
		, name_(name)
	{}

	virtual void init(Block * parentBlock)
	{
		Expression::init(parentBlock);
		variable_ = parentBlock->resolveVariable(name_);
		if(!variable_)
		{
			throw VariableNotFoundException(name_, location());
		}
	}

	virtual ptr<Value> evaluate() const
	{
		return variable_->value();
	}
	
	virtual Value::DataType dataType() const
	{
		return variable_->dataType();
	}
private:
	String name_;
	ptr<Variable> variable_;
};

#endif //__MCS__VARIABLE_EXPRESSION__HPP
